| Conditions | 2 |
| Paths | 0 |
| Total Lines | 340 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // spec/helpers/hierarchy-helper.js |
||
| 9 | module.exports = (hierarchy) => { |
||
| 10 | // check parameters |
||
| 11 | if (!Array.isArray(hierarchy)) { |
||
| 12 | throw new Error("hierarchy must be an array"); |
||
| 13 | } |
||
| 14 | |||
| 15 | // load dependencies |
||
| 16 | let deps = hierarchy.map((value, key, arr) => { |
||
| 17 | return require(path.join(root, value + ".js")); |
||
| 18 | }); |
||
| 19 | |||
| 20 | let klassName = hierarchy.pop(); |
||
| 21 | let klass = deps.pop(); |
||
| 22 | const first = (hierarchy.length === 0); |
||
| 23 | const third = (hierarchy.length >= 3); |
||
| 24 | |||
| 25 | // :: TESTING |
||
| 26 | |||
| 27 | // test the last class in the hierarchy tree |
||
| 28 | describe(klassName, () => { |
||
| 29 | |||
| 30 | // :: INHERITED PROTOTYPE |
||
| 31 | |||
| 32 | // all inherit from Object |
||
| 33 | it("should inherit from 'Object'", () => { |
||
| 34 | expect(new klass()).toEqual(jasmine.any(Object)); |
||
| 35 | }); |
||
| 36 | |||
| 37 | // check the hierarchy tree |
||
| 38 | for (let i = 0; i < hierarchy.length; i += 1) { |
||
| 39 | it("should inherit from '" + hierarchy[i] + "'", () => { |
||
| 40 | expect(new klass()).toEqual(jasmine.any(deps[i])); |
||
| 41 | }); |
||
| 42 | } |
||
| 43 | |||
| 44 | // check inherited properties |
||
| 45 | if (!first) { |
||
| 46 | it("should have a prototype property named 'name'", () => { |
||
| 47 | expect(klass.prototype).toHaveString("name"); |
||
| 48 | }); |
||
| 49 | |||
| 50 | it("should have a prototype property named 'message'", () => { |
||
| 51 | expect(klass.prototype).toHaveString("message"); |
||
| 52 | }); |
||
| 53 | |||
| 54 | it("should have a prototype property named 'code'", () => { |
||
| 55 | expect(klass.prototype).toHaveMember("code"); |
||
| 56 | }); |
||
| 57 | } |
||
| 58 | |||
| 59 | // check Object methods |
||
| 60 | it("should have a prototype method named 'toString()'", () => { |
||
| 61 | expect(klass.prototype).toHaveMethod("toString"); |
||
| 62 | }); |
||
| 63 | |||
| 64 | // check inherited methods |
||
| 65 | if (!first) { |
||
| 66 | it("should have a prototype method named 'native()'", () => { |
||
| 67 | expect(klass.prototype).toHaveMethod("native"); |
||
| 68 | }); |
||
| 69 | } |
||
| 70 | |||
| 71 | // :: EXTENDED PROTOTYPE |
||
| 72 | |||
| 73 | // check extended properties |
||
| 74 | if (first) { |
||
| 75 | it("should have a prototype property named 'name'", () => { |
||
| 76 | expect(klass.prototype).toHaveString("name"); |
||
| 77 | }); |
||
| 78 | |||
| 79 | it("should have a prototype property named 'message'", () => { |
||
| 80 | expect(klass.prototype).toHaveString("message"); |
||
| 81 | }); |
||
| 82 | |||
| 83 | it("should have a prototype property named 'code'", () => { |
||
| 84 | expect(klass.prototype).toHaveMember("code"); |
||
| 85 | }); |
||
| 86 | |||
| 87 | it("should have a prototype method named 'native()'", () => { |
||
| 88 | expect(klass.prototype).toHaveMethod("native"); |
||
| 89 | }); |
||
| 90 | } |
||
| 91 | |||
| 92 | // :: PROTOTYPE VALUES |
||
| 93 | |||
| 94 | it("should have the 'class' name in the prototype property named 'name'", () => { |
||
| 95 | expect(klass.prototype.name).toEqual(klassName); |
||
| 96 | }); |
||
| 97 | |||
| 98 | it("should have a dummy default value as message", () => { |
||
| 99 | expect(klass.prototype.message).toEqual("thrown"); |
||
| 100 | }); |
||
| 101 | |||
| 102 | it("should have a null default value as code", () => { |
||
| 103 | expect(klass.prototype.code).toBeNull(); |
||
| 104 | }); |
||
| 105 | |||
| 106 | // :: CONSTRUCTOR |
||
| 107 | |||
| 108 | it("should instantiate without parameters", () => { |
||
| 109 | let arg1, arg2, arg3, test; |
||
| 110 | if (third) test = (() => new klass(arg1, arg2)); |
||
| 111 | else test = (() => new klass(arg1, arg2, arg3)); |
||
| 112 | for (let i = 0; i < 2; i += 1) { |
||
| 113 | arg1 = (i % 2 === 0 ? undefined : null); |
||
| 114 | for (let j = 0; j < 2; j += 1) { |
||
| 115 | arg2 = (j % 2 === 0 ? undefined : null); |
||
| 116 | if (third) { |
||
| 117 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 118 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 119 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 120 | } else { |
||
| 121 | for (let e = 0; e < 2; e += 1) { |
||
| 122 | arg3 = (e % 2 === 0 ? undefined : null); |
||
| 123 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 124 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 125 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | test = (() => new klass()); |
||
| 131 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 132 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 133 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 134 | }); |
||
| 135 | |||
| 136 | it("should instantiate with parameters", () => { |
||
| 137 | let arg1, arg2, arg3, test1, test2, test3, args1, args2, args3; |
||
| 138 | test1 = (() => new klass(arg1)); |
||
| 139 | test2 = (() => new klass(arg1, arg2)); |
||
| 140 | if (!third) { |
||
| 141 | test3 = (() => new klass(arg1, arg2, arg3)); |
||
| 142 | args1 = [undefined, null, klass.prototype.name]; |
||
| 143 | args2 = [undefined, null, klass.prototype.message]; |
||
| 144 | args3 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
||
| 145 | } else { |
||
| 146 | args1 = [undefined, null, klass.prototype.message]; |
||
| 147 | args2 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
||
| 148 | } |
||
| 149 | for (let i = 0; i < args1.length; i += 1) { |
||
| 150 | arg1 = args1[i]; |
||
| 151 | expect(test1).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 152 | expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 153 | expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 154 | for (let j = 0; j < args2.length; j += 1) { |
||
| 155 | arg2 = args2[j]; |
||
| 156 | expect(test2).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 157 | expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 158 | expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 159 | if (!third) { |
||
| 160 | for (let e = 0; e < args3.length; e += 1) { |
||
| 161 | arg3 = args3[e]; |
||
| 162 | expect(test3).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 163 | expect(test3).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 164 | expect(test3).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | }); |
||
| 170 | |||
| 171 | it("should throw an Error if 'message' or 'code' are invalid parameters", () => { |
||
| 172 | let arg1, arg2, arg3, test31, test32, test33, test21, test22, test11; |
||
| 173 | const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
||
| 174 | const noNmb = [{}, true, false, '', "qwerty", () => null]; |
||
| 175 | const noStrSz = noStr.length; |
||
| 176 | const noNmbSz = noNmb.length; |
||
| 177 | if (!third) { |
||
| 178 | test33 = (() => new klass(arg1, arg2, arg3)); |
||
| 179 | test32 = (() => new klass(null, arg2, arg3)); |
||
| 180 | test31 = (() => new klass(null, null, arg3)); |
||
| 181 | } |
||
| 182 | test22 = (() => new klass(arg1, arg2)); |
||
| 183 | test21 = (() => new klass(null, arg2)); |
||
| 184 | test11 = (() => new klass(arg1)); |
||
| 185 | if (typeof Symbol === "function") { |
||
| 186 | noStr.push(Symbol("symbol")); |
||
| 187 | noNmb.push(Symbol("symbol")); |
||
| 188 | } |
||
| 189 | for (let i = 0; i < noStrSz; i += 1) { |
||
| 190 | arg1 = noStr[i]; |
||
| 191 | if (third) expect(test11).toThrowError("parameter 'message' must be a 'string'"); |
||
| 192 | else expect(test11).toThrowError("parameter 'name' must be a 'string'"); |
||
| 193 | for (let j = 0; j < (third ? noNmbSz : noStrSz); j += 1) { |
||
| 194 | if (hierarchy.length > 2) { |
||
| 195 | arg2 = noNmb[j]; |
||
| 196 | expect(test22).toThrowError("parameter 'message' must be a 'string'"); |
||
| 197 | expect(test21).toThrowError("parameter 'code' must be a 'number'"); |
||
| 198 | } else { |
||
| 199 | arg2 = noStr[j]; |
||
| 200 | expect(test22).toThrowError("parameter 'name' must be a 'string'"); |
||
| 201 | expect(test21).toThrowError("parameter 'message' must be a 'string'"); |
||
| 202 | for (let e = 0; e < noNmb.length; e += 1) { |
||
| 203 | arg3 = noNmb[e]; |
||
| 204 | expect(test33).toThrowError("parameter 'name' must be a 'string'"); |
||
| 205 | expect(test32).toThrowError("parameter 'message' must be a 'string'"); |
||
| 206 | expect(test31).toThrowError("parameter 'code' must be a 'number'"); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | }); |
||
| 212 | |||
| 213 | // :: MEMBER PROPERTIES |
||
| 214 | |||
| 215 | let name; |
||
| 216 | const message = "asdf"; |
||
| 217 | const code = Math.round(Math.random() * 0xFFFFFFFF); |
||
| 218 | if (!third) name = "qwerty"; |
||
| 219 | |||
| 220 | it("should have all correct properties once instantiated", () => { |
||
| 221 | for (let i = 0; i < 2; i += 1) { |
||
| 222 | const even1 = (i % 2 === 0); |
||
| 223 | const arg1 = (even1 ? (third ? message : name) : null); |
||
| 224 | const source1 = new klass(arg1); |
||
| 225 | if (even1) { |
||
| 226 | if (third) { |
||
| 227 | expect(source1.name).toEqual(klass.prototype.name); |
||
| 228 | expect(source1.message).toEqual(message); |
||
| 229 | } else { |
||
| 230 | expect(source1.name).toEqual(name); |
||
| 231 | expect(source1.message).toEqual(klass.prototype.message); |
||
| 232 | } |
||
| 233 | } else { |
||
| 234 | expect(source1.name).toEqual(klass.prototype.name); |
||
| 235 | expect(source1.message).toEqual(klass.prototype.message); |
||
| 236 | } |
||
| 237 | expect(source1.code).toBeNull(); |
||
| 238 | for (let j = 0; j < 2; j += 1) { |
||
| 239 | const even2 = (j % 2 === 0); |
||
| 240 | const arg2 = (even2 ? (third ? code : message) : null); |
||
| 241 | const source2 = new klass(arg1, arg2); |
||
| 242 | if (third) { |
||
| 243 | expect(source2.name).toEqual(klass.prototype.name); |
||
| 244 | if (even1) expect(source2.message).toEqual(message); |
||
| 245 | else expect(source2.message).toEqual(klass.prototype.message); |
||
| 246 | if (even2) expect(source2.code).toEqual(code); |
||
| 247 | else expect(source2.code).toBeNull(); |
||
| 248 | } else { |
||
| 249 | if (even1) expect(source2.name).toEqual(name); |
||
| 250 | else expect(source2.name).toEqual(klass.prototype.name); |
||
| 251 | if (even2) expect(source2.message).toEqual(message); |
||
| 252 | else expect(source2.message).toEqual(klass.prototype.message); |
||
| 253 | expect(source2.code).toBeNull(); |
||
| 254 | for (let e = 0; e < 2; e += 1) { |
||
| 255 | const even3 = (e % 2 === 0); |
||
| 256 | const arg3 = (even3 ? code : null); |
||
| 257 | const source3 = new klass(arg1, arg2, arg3); |
||
| 258 | if (even1) expect(source3.name).toEqual(name); |
||
| 259 | else expect(source3.name).toEqual(klass.prototype.name); |
||
| 260 | if (even2) expect(source3.message).toEqual(message); |
||
| 261 | else expect(source3.message).toEqual(klass.prototype.message); |
||
| 262 | if (even3) expect(source3.code).toEqual(code); |
||
| 263 | else expect(source3.code).toBeNull(); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | }); |
||
| 269 | |||
| 270 | // :: MEMBER METHODS |
||
| 271 | |||
| 272 | it("#toString()", () => { |
||
| 273 | for (let i = 0; i < 2; i += 1) { |
||
| 274 | let exp1; |
||
| 275 | const even1 = (i % 2 === 0); |
||
| 276 | const arg1 = (even1 ? (third ? message : name) : null); |
||
| 277 | const source1 = new klass(arg1); |
||
| 278 | if (even1) { |
||
| 279 | if (third) exp1 = klass.prototype.name + ": " + message + '.'; |
||
| 280 | else exp1 = name + ": " + klass.prototype.message + '.'; |
||
| 281 | } else { |
||
| 282 | exp1 = klass.prototype.name + ": " + klass.prototype.message + '.'; |
||
| 283 | } |
||
| 284 | expect(source1.toString()).toEqual(exp1); |
||
| 285 | for (let j = 0; j < 2; j += 1) { |
||
| 286 | let exp2; |
||
| 287 | const even2 = (j % 2 === 0); |
||
| 288 | const arg2 = (even2 ? (third ? code : message) : null); |
||
| 289 | const source2 = new klass(arg1, arg2); |
||
| 290 | if (third) { |
||
| 291 | exp2 = klass.prototype.name; |
||
| 292 | exp2 += (even2 ? " (0x" + code.toString(16) + "):" : ':' ) + ' '; |
||
| 293 | exp2 += (even1 ? message : klass.prototype.message) + '.'; |
||
| 294 | } else { |
||
| 295 | exp2 = (even1 ? name : klass.prototype.name) + ':'; |
||
| 296 | exp2 += ' ' + (even2 ? message : klass.prototype.message) + '.'; |
||
| 297 | } |
||
| 298 | expect(source2.toString()).toEqual(exp2); |
||
| 299 | if (!third) { |
||
| 300 | for (let e = 0; e < 2; e += 1) { |
||
| 301 | let exp3; |
||
| 302 | const even3 = (e % 2 === 0); |
||
| 303 | const arg3 = (even3 ? code : null); |
||
| 304 | const source3 = new klass(arg1, arg2, arg3); |
||
| 305 | exp3 = (even1 ? name : klass.prototype.name); |
||
| 306 | exp3 += (even3 ? " (0x" + code.toString(16) + "):" : ':') + ' '; |
||
| 307 | exp3 += (even2 ? message : klass.prototype.message) + '.'; |
||
| 308 | expect(source3.toString()).toEqual(exp3); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | }); |
||
| 314 | |||
| 315 | it("#native()", () => { |
||
| 316 | for (let i = 0; i < 2; i += 1) { |
||
| 317 | let exp1; |
||
| 318 | const even1 = (i % 2 === 0); |
||
| 319 | const arg1 = (even1 ? (third ? message : name) : null); |
||
| 320 | const source1 = new klass(arg1); |
||
| 321 | exp1 = (even1 && third ? message : klass.prototype.message); |
||
| 322 | expect(source1.native()).toEqual(new Error(exp1)); |
||
| 323 | for (let j = 0; j < 2; j += 1) { |
||
| 324 | let exp2; |
||
| 325 | const even2 = (j % 2 === 0); |
||
| 326 | const arg2 = (even2 ? (third ? code : message) : null); |
||
| 327 | const source2 = new klass(arg1, arg2); |
||
| 328 | if (third) { |
||
| 329 | exp2 = (even1 ? message : klass.prototype.message); |
||
| 330 | expect(source2.native()).toEqual(new Error(exp2)); |
||
| 331 | } else { |
||
| 332 | exp2 = (even2 ? message : klass.prototype.message); |
||
| 333 | expect(source2.native()).toEqual(new Error(exp2)); |
||
| 334 | for (let e = 0; e < 2; e += 1) { |
||
| 335 | let exp3; |
||
| 336 | const even3 = (e % 2 === 0); |
||
| 337 | const arg3 = (even3 ? code : null); |
||
| 338 | const source3 = new klass(arg1, arg2, arg3); |
||
| 339 | exp3 = (even2 ? message : klass.prototype.message); |
||
| 340 | expect(source3.native()).toEqual(new Error(exp3)); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | }); |
||
| 346 | }); |
||
| 347 | |||
| 348 | }; |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.